package com.future.security.controller;

import com.alibaba.fastjson.JSON;
import com.future.security.entity.TaskInfo;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author yinhao
 * @Date 2021/3/27 17:55
 * @Description
 */
@RestController
public class ParseDataController {

    @RequestMapping("/hello")
    public List<TaskInfo> getData() {
        Map<String,String> map = new HashMap<>();
        map.put("taskName","测试");
        map.put("systemId","1");
        map.put("areaCode","330603000000");
        String data = timeTask("http://122.224.35.128:10000/mh/taskInfo/searchListByTaskName",
                "eyJhbGciOiJIUzI1NiJ9.eyJkZXBhcnRtZW50TmFtZSI6Iuafr-ahpeWMuuWkp-aVsOaNruWPkeWxleeuoeeQhuS4reW_gyzkuK3lhbHnu43lhbTluILmn6_moaXljLrlp5TmlL_ms5Xlp5TlkZjkvJoiLCIkaW50X3Blcm1zIjpbIkFkZFRhc2tRdWVzdGlvbiIsIkFkZFRhc2tQb2xpY3kiLCJBZGRUYXNrRHluYW1pYyIsInBlcm1pc3Npb25NYW5hZ2UiLCJiYWNrIiwiRWRpdE1pbGUiLCJ0YXNrTWFuYWdlIiwiVGFza0ZpbmlzaCIsIlVwbG9hZFRhc2tSZXBvcnRlciIsIkRlbGV0ZVJlZm9ybSIsIlVwbG9hZFRhc2siLCJBZGRXb3JrUGxhbiIsIkVkaXRUYXNrIiwiQWRkUXVvdGEiLCJwb3J0YWwiLCJmcm9udEluZGV4IiwiRWRpdFJlZm9ybSIsIkVkaXRXb3JrUGxhbiIsIlZpZXdNaWxlIiwiQWRkVGFzayIsIkFkZENoaWxkVGFzayIsIkFkZFJl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UxODE1In0.dBBHpyTjV925TFS1yJKYpNQA8oQepudY8j03W6hsGPQ",
                map);
        Map dataMap = JSON.parseObject(data, Map.class);
        return (List<TaskInfo>) dataMap.get("data");
    }

    public String timeTask(String url,String token,Map<String,String> qryMap) {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        String response = null;

        List<NameValuePair> pairs = new ArrayList<>();
        for (Map.Entry<String, String> entity : qryMap.entrySet()) {
            pairs.add(new BasicNameValuePair(entity.getKey(),entity.getValue()));
        }

        try {
            URIBuilder builder = new URIBuilder(url);
            builder.setParameters(pairs);
            HttpGet get = new HttpGet(builder.build());
            get.setHeader("token",token);
            get.setHeader("CharacterEncoding","UTF-8");

            HttpResponse res = httpClient.execute(get);
            response = EntityUtils.toString(res.getEntity(),"UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;


    }


}
